import {columnMap} from './predefined-table';

const cols = {
  'updateCol': {desc:'调整后列名', width: 250, isFilterable: true},
  'sourceCol': {desc:'原始数据列名', width: 250, isFilterable: true},
  'handle': {desc:'处理方法', width: 250,},
};

const procDict = {
  
  NUMBER(orig){
    if (orig === undefined) {
      return 0;
    } else if (typeof orig === 'string') {
      return parseFloat(orig.replace(/[\s,]/g, ''));
    } else {
      return orig;
    }
  },
  
  DIRECTION(orig){
    return {
      Dr: 'dr',
      Cr: 'cr',
      借: 'dr',
      贷: 'cr',
    }[orig]
  },
  
  EXCEL_DATE(dateString){
    return new Date(Math.round((dateString - 25569)*86400*1000));
  },

}

const cookMap = (origMap, procMap) => {

  const entries = origMap
    .map(({sourceCol, updateCol, handle}) => {
      return [sourceCol.replace(/\s/g, ''), {key: updateCol, func: procMap[handle]}]
    })

  return Object.fromEntries(entries);
}

const procData = (list) => {
  return list;
}

const procEval = (books) => {

  const book = books.find(({name}) => name === 'COLUMNS');
  console.log('EVAL', book.data)
  book.map = cookMap(book.data, procDict);

  console.log(book.map);

}

export default {
  name: 'COLUMNS',
  descAddi: 'column header mapping',
  desc: '数据列名归一化',
  cate: '系统',
  cols,
  data: procData(columnMap),
  dataTool: {
    useImport: {
      dataSpecs: [
        {
          name: 'COLUMNS',
          desc: '数据列名映射表'
        },
      ],
      proc: procData,
    },
  },
  eval: {
    autoEval: true,
    proc: procEval
  }
}
